package de.tuberlin.project.oose.figures.fields;

import static org.jhotdraw.draw.AttributeKeys.TRANSFORM;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JTextField;

import org.jhotdraw.draw.DrawingView;
import org.jhotdraw.draw.TextHolderFigure;
import org.jhotdraw.draw.event.FigureAdapter;
import org.jhotdraw.draw.event.FigureEvent;
import org.jhotdraw.draw.event.FigureListener;




public  class OurFloatingTextField {
    private TextHolderFigure editedFigure;
    private JTextField   textField;
    private DrawingView   view;
    private FigureListener figureHandler = new FigureAdapter() {
        @Override
        public void attributeChanged(FigureEvent e) {
            updateWidget();
        }
    };
    
    public OurFloatingTextField() {
        textField = new JTextField(20);
    }
    
    public void requestFocus() {
        textField.requestFocus();
    }
    
    /**
     * Creates the overlay for the given Container using a
     * specific font.
     */
    public void createOverlay(DrawingView view, TextHolderFigure figure) {
        view.getComponent().add(textField, 0);
        textField.setText(figure.getText());
        textField.setColumns(figure.getTextColumns());
        textField.selectAll();
        textField.setVisible(true);
        editedFigure = figure;
        editedFigure.addFigureListener(figureHandler);
        this.view = view;
        updateWidget();
    }
    
    protected void updateWidget() {
        Font font = editedFigure.getFont();
        font = font.deriveFont(font.getStyle(), (float) (editedFigure.getFontSize() * view.getScaleFactor()));
        textField.setFont(font);
        textField.setForeground(editedFigure.getTextColor());
        textField.setBackground(editedFigure.getFillColor());

        Rectangle2D.Double fDrawBounds = editedFigure.getBounds();
        Point2D.Double fDrawLoc = new Point2D.Double(fDrawBounds.getX(), fDrawBounds.getY());
        if (editedFigure.get(TRANSFORM) != null) {
        editedFigure.get(TRANSFORM).transform(fDrawLoc, fDrawLoc);
        }
        Point fViewLoc = view.drawingToView(fDrawLoc);
        Rectangle fViewBounds = view.drawingToView(fDrawBounds);
        fViewBounds.x = fViewLoc.x;
        fViewBounds.y = fViewLoc.y;
        Dimension tfDim = textField.getPreferredSize();
        Insets tfInsets = textField.getInsets();
        float fontBaseline = textField.getGraphics().getFontMetrics(font).getMaxAscent();
        double fBaseline = editedFigure.getBaseline() * view.getScaleFactor();
        textField.setBounds(
                fViewBounds.x - tfInsets.left,
                fViewBounds.y - tfInsets.top - (int) (fontBaseline - fBaseline),
                Math.max(fViewBounds.width + tfInsets.left + tfInsets.right, tfDim.width),
                Math.max(fViewBounds.height + tfInsets.top + tfInsets.bottom, tfDim.height)
                );
    }
    
    public Insets getInsets() {
        return textField.getInsets();
    }
    
    /**
     * Adds an action listener
     */
    public void addActionListener(ActionListener listener) {
        textField.addActionListener(listener);
    }
    
    /**
     * Remove an action listener
     */
    public void removeActionListener(ActionListener listener) {
        textField.removeActionListener(listener);
    }
    
    
    /**
     * Gets the text contents of the overlay.
     */
    public String getText() {
        return textField.getText();
    }
    
    /**
     * Gets the preferred size of the overlay.
     */
    public Dimension getPreferredSize(int cols) {
        textField.setColumns(cols);
        textField.setPreferredSize(new Dimension(50,20));
        return textField.getPreferredSize();
    }
    
    /**
     * Removes the overlay.
     */
    public void endOverlay() {
        view.getComponent().requestFocus();
        if (textField != null) {
            textField.setVisible(false);
            view.getComponent().remove(textField);
            
            Rectangle bounds = textField.getBounds();
            view.getComponent().repaint(bounds.x, bounds.y, bounds.width, bounds.height);
        }
        if (editedFigure != null) {
            editedFigure.removeFigureListener(figureHandler);
            editedFigure = null;
        }
    }
}

